function y = custom_filter(b, a, x)
    %   Inputs:
    %       b - Numerator coefficients of the filter
    %       a - Denominator coefficients of the filter
    %       x - Input signal
    %   Output:
    %       y - Filtered output signal

    % Ensure coefficients are column vectors
    b = b(:);
    a = a(:);
    % Ensure input is a column vector
    x = x(:);
    % Normalize the coefficients (if a(1) is not 1)
    if a(1) ~= 1
        b = b / a(1);
        a = a / a(1);
    end
    % Length of input and filter coefficients
    N = length(x); % Length of input signal
    nb = length(b); % Order of numerator
    na = length(a); % Order of denominator
    % Initialize output and state variables
    y = zeros(N, 1); % Output signal
    w = zeros(max(na, nb), 1); % State vector (intermediate values)
    % Main filtering loop
    for n = 1:N
        % Update the state variable
        w(1) = x(n) - sum(a(2:end) .* w(2:na));
        % Compute the output
        y(n) = sum(b .* w(1:nb));
        % Shift the state variable
        w(2:end) = w(1:end-1);
    end
end
